<?php
/**
 * A very light framework built on webappkit kits structure
 *
 * container for HTTP response elements
 * including intermediate elements such as user, language etc
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package webappkit
 * @subpackage engine
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * @property HttpRequestBean $request
 * @property string $view view to render
 */
class WakEngineResponse extends DataBean {

    function __toString() {
        return 'WakEngineResponse object';
    }

    /**
     * retrieve the currently processed node
     * and increments the node index (currentNode)
     * defaults to 'index'
     * @access public
     * @return string
     */
    public function shiftNode() {
        $node=$this->get('currentNode');
        $this->currentNode++;
        return $node;
    }

    /**
     * What will be the next uri segments to process
     * @acces public
     * @return array
     */
    public function getNextNodes() {
        // print_r($this->currentNode);
        return array_slice($this->nodes,$this->currentNode);
    }

    /**
     * Defines lang from a choose list
     * picks the highest ranked client accept-language from the list
     * or uses the first item in list as default
     * @access public
     * @param array $provided language codes
     * @param string $default default language to use if no match is found
     * @return bool
     */
    public function chooseLangFrom($list, $default=null) {
        // looping through rank-ordered client accept-language
        // echo '<pre>'; print_r($this->request->accept_language);
        foreach ($this->request->accept_language as $user_pick) {
            if (in_array($user_pick,$list)) {
                $this->lang=$user_pick;
                return true;
            }
        }
        // user picks not provided
        if ($default)
            $this->lang=$default;
        return false;
    }

    /**
    * sends http headers
    */
    function sendHeaders() {
        // status
        header($this->_statusLine());
        // http realm
    }

    /**
     * @var HttpRequestBean
     */
    public $request;
    protected function _request_settable() {
        return false;
    }
    protected function _request_autoValue() {
        return HttpRequestBean::getCurrent();
    }

    /**
     * @var UrlBean
     */
    public $url;
    protected function _url_autoValue() {
        return $this->request->url;
    }

    /**
     * @var string base href
     */
    public $base_href;
    protected function _base_href_autoValue() {
        return $this->request->getBaseHref();
    }

    /**
     * @var string current language code
     */
    public $lang;
    function _lang_autoValue() {
        $langs = $this->request->get('accept_language'); //print_r($langs);
        return current($langs);
    }

    /**
     * @var array uri segments
     */
    public $nodes;
    function _nodes_autoValue() {
        return $this->url->splitPath();
    }

    /**
     * @var int 0-n current node index
     */
    public $currentNode;
    function _currentNode_autoValue() {
        return 0;
    }
    function _currentNode_get() {
        if (!isset($this->nodes[$this->currentNode])
        or !$node=$this->nodes[$this->currentNode])
            $node='index';
        return $node;
    }

    /**
     * @var string view's id to render current node
     * defaults to current node's name
     */
    public $view;
    function _view_autoValue() {
        return $this->get('currentNode');
    }

    /**
    * @var int http response status code
    */
    var $status=200;
    function _status_inputFilter($x) {
        return $this->_toInteger($x);
    }
    function _status_check($x) {
        return in_array($x,array(
            100,101
            ,200,201,202,203,204,205,206
            ,300,301,302,303,304,305,306,307
            ,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,415,416,417
            ,500,501,502,503,504,505));
    }
    /**
    * @var array status rearsoins strings code => reason
    */
    var $_status_reasons=array(
        '100' => 'Continue'
        , '101' => 'Switching Protocols'
        , '200'  => 'OK'
        , '201'  => 'Created'
        , '202'  => 'Accepted'
        , '203'  => 'Non-Authoritative Information'
        , '204'  => 'No Content'
        , '205'  => 'Reset Content'
        , '206'  => 'Partial Content'
        , '300'  => 'Multiple Choices'
        , '301'  => 'Moved Permanently'
        , '302'  => 'Found'
        , '303'  => 'See Other'
        , '304'  => 'Not Modified'
        , '305'  => 'Use Proxy'
        , '307'  => 'Temporary Redirect'
        , '400'  => 'Bad Request'
        , '401'  => 'Unauthorized'
        , '402'  => 'Payment Required'
        , '403'  => 'Forbidden'
        , '404'  => 'Not Found'
        , '405'  => 'Method Not Allowed'
        , '406'  => 'Not Acceptable'
        , '407'  => 'Proxy Authentication Required'
        , '408'  => 'Request Time-out'
        , '409'  => 'Conflict'
        , '410'  => 'Gone'
        , '411'  => 'Length Required'
        , '412'  => 'Precondition Failed'
        , '413'  => 'Request Entity Too Large'
        , '414'  => 'Request-URI Too Large'
        , '415'  => 'Unsupported Media Type'
        , '416'  => 'Requested range not satisfiable'
        , '417'  => 'Expectation Failed'
        , '500'  => 'Internal Server Error'
        , '501'  => 'Not Implemented'
        , '502'  => 'Bad Gateway'
        , '503'  => 'Service Unavailable'
        , '504'  => 'Gateway Time-out'
        , '505'  => 'HTTP Version not supported');

    /**
    * builds status line header
    * @return string
    */
    function _statusLine() {
        return 'HTTP/1.1 '.$this->status.' '.$this->_status_reasons[$this->status];
    }

    /**
     * may be used to display breadcrumb links
     * @var array label => url (relative)
     */
    public $breadcrumb=array();

    public function addBreadCrumb($label,$url) {
        $this->breadcrumb[$label]=$url;
    }
}